package jwt;

import org.junit.Test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public class BuildJwt {

    public static final String SECRET_KEY = "zhuzhuqianguan2018";

    @Test
    public void create() {
        String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .claim("userId", 89)
                .compact();
        System.out.println(jwt);
    }

    @Test
    public void jud() {
        String jwtStr = "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI4OSJ9.nVLJ8NoKBQIhFzpMiKm89LQFXpx4jknM_qvEV34BIOM";
        Claims claims = Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(jwtStr).getBody();
        String userId = claims.get("userId", String.class);
        System.out.println(userId);
    }
}
